package com.hzx.demo.activity;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.hzx.demo.R;
import com.hzx.demo.helper.VideoModule;
import com.hzx.demo.util.Entry;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.io.File;

@Entry(desc = "自定义录像器", createTime = "2016-06-22")
@EActivity(R.layout.activity_video_capture)
public class VideoCaptureActivity extends AppCompatActivity implements SurfaceHolder.Callback {
    @SuppressWarnings("unused")
    private static final String TAG = "VideoCaptureActivity";

    @ViewById(R.id.sv_camera_preview)
    protected SurfaceView mCameraPreviewSurface;
    private VideoModule mVideoModule = new VideoModule();


    @AfterViews
    protected void afterView(){
        mCameraPreviewSurface.getHolder().addCallback(this);
    }



    @Click(R.id.btn_start_recorder)
    protected void startCaptureVideo(){
        mVideoModule.startRecorder(getPath());
    }


    @Click(R.id.btn_stop_recorder)
    protected void stopRecorder(){
        mVideoModule.stopRecorderAndReleaseMediaRecorder();
        mVideoModule.stopPreviewAndCloseCamera();
    }

    private String getPath(){
        String path =  Environment.getExternalStorageDirectory() + "/hzxdemo/testCaptureVideo.mp4";
        File file = new File(path);
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        return path;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mVideoModule.openCamera(false, mCameraPreviewSurface);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}
